package com.geek.config.microprofile.source;

import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class MapBaseConfigSource implements ConfigSource {

  private final Map<String, String> configData;
  private final String name;
  private final int ordinal; //序数

  public MapBaseConfigSource(final String name, final int ordinal) {
    this.configData = new HashMap<>();
    this.name = name;
    this.ordinal = ordinal;
  }

  public Map<String, String> getConfigData() {
    if (configData.isEmpty()) {
      prepareConfigData(configData);
    }
    return configData;
  }

  /*
   * 准备配置数据
   */
  protected abstract void prepareConfigData(final Map<String, String> configData);

  @Override
  public Map<String, String> getProperties() {
    return ConfigSource.super.getProperties();
  }

  @Override
  public Set<String> getPropertyNames() {
    return this.configData.keySet();
  }

  @Override
  public int getOrdinal() {
//    return ConfigSource.super.getOrdinal();
    return this.ordinal;
  }

  @Override
  public String getValue(final String key) {
    return this.configData.get(key);
  }

  @Override
  public String getName() {
    return this.name;
  }
}
